﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    public class AuthorizationDAO : DAOBase, IAuthorizationDAO
    {
        #region IAuthorizationDAO Members

        public CollectionFeedback<Authorization> GetCollection(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Authorization> {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("GetCollectionAuthorization", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                OpenConnection();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    var authentications = new List<Authorization>();
                    while (reader.Read())
                    {
                        var authorization = new Authorization();

                        authorization.AuthorizationID = Convert.ToInt32(reader["AuthorizationID"]);
                        authorization.Description = reader["Description"].ToString();

                        authentications.Add(authorization);
                    }

                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                    feedback.Data = authentications;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                    feedback.IsSuccessful = true;
                    feedback.Data = null;
                }

                //Log.Info("GetAuthorizationCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("GetAuthorizationCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("GetAuthorizationCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Authorization> GetCollectionType(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Authorization> {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("GetCollectionAuthorization", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                OpenConnection();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    var authentications = new List<Authorization>();
                    while (reader.Read())
                    {
                        var authorization = new Authorization();

                        authorization.AuthorizationID = Convert.ToInt32(reader["AuthorizationID"]);
                        authorization.Description = reader["Description"].ToString();

                        authentications.Add(authorization);
                    }

                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                    feedback.Data = authentications;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                    feedback.IsSuccessful = true;
                    feedback.Data = null;
                }

                //Log.Info("GetAuthorizationCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("GetAuthorizationCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("GetAuthorizationCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<Authorization> GetSingle(int objectID)
        {
            throw new NotImplementedException();
        }

        public SingleFeedback<int> Insert(Authorization data)
        {
            throw new NotImplementedException();
        }

        public Feedback Update(Authorization data)
        {
            throw new NotImplementedException();
        }

        public Feedback Delete(int objID)
        {
            throw new NotImplementedException();
        }

        public CollectionFeedback<Authorization> Search(Authorization criteria)
        {
            throw new NotImplementedException();
        }

        public Feedback UndoDelete(int objID)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}